package roborally;

public class Facade implements IFacade{

	@Override
	public IRobot createRobot(long x, long y, int orientation, double energy) {
		try{
			IRobot robot = new Robot(x,y,orientation,energy);
			return robot;
		} catch(IllegalArgumentException exc){
			return null;
		}
	}

	@Override
	public long getX(IRobot robot) {
		return robot.getxPos();
	}

	@Override
	public long getY(IRobot robot) {
		return robot.getyPos();
	}

	@Override
	public int getOrientation(IRobot robot) {
		return robot.getDirectionNumber();
	}

	@Override
	public void move(IRobot robot) {
		robot.move();
	}

	@Override
	public void turnClockwise(IRobot robot) {
		robot.turnClockwise();
	}
	
	@Override
	public void turnCounterClockwise(IRobot robot) {
		robot.turnCounterClockwise();
	}

	@Override
	public double getEnergy(IRobot robot) {
		return robot.getEnergy();
	}

	@Override
	public void recharge(IRobot robot, double energyAmount) {
		robot.recharge(energyAmount);
		
	}

	@Override
	public int isGetEnergyRequiredToReachAndMoveNextTo16Plus() {
		return 1;
	}

	@Override
	public double getEnergyRequiredToReach(IRobot robot, long x, long y) {
		return robot.getEnergyRequiredToReach(x, y, isGetEnergyRequiredToReachAndMoveNextTo16Plus() == 1);
	}

	@Override
	public void moveNextTo(IRobot robot, IRobot robot2) {
		if(isGetEnergyRequiredToReachAndMoveNextTo16Plus() == 0){
			robot.moveNextTo(robot2.getxPos(), robot2.getyPos(), false);
			robot2.moveNextTo(robot.getxPos(), robot.getyPos(), false);
		}else{
			if(robot.getPriority(robot2.getxPos(), robot2.getyPos()) <= 
					robot2.getPriority(robot.getxPos(), robot.getyPos())){
				while(robot.getPriority(robot2.getxPos(), robot2.getyPos()) <=
						robot2.getPriority(robot.getxPos(), robot.getyPos())){
					if(robot.getDistanceTo(robot2.getxPos(), robot2.getyPos())==1){
						break;
					}
					robot.moveNextTo(robot2.getxPos(), robot2.getyPos(), true);
				}
				robot2.moveNextTo(robot.getxPos(), robot.getyPos(), false);
			}else{
				while(robot.getPriority(robot2.getxPos(), robot2.getyPos()) >=
						robot2.getPriority(robot.getxPos(), robot.getyPos())){
					if(robot.getDistanceTo(robot2.getxPos(), robot2.getyPos())==1){
						break;
					}
					robot2.moveNextTo(robot.getxPos(), robot.getyPos(), true);
				}
				robot.moveNextTo(robot2.getxPos(), robot2.getyPos(), false);
			}
		}
	}
}
